<?php
include 'redirectAdmin.php';

$action = "find";
if (isset ( $_GET ["action"] )) {
	$action = $_GET ["action"];
}

?>
<table class="manage-service">
	<tr>
		<td class="left-menu">
			<div><a href="bookmarks.php?page=manageUser&action=find">Odszukaj</a></div><br/>
			<div><a href="bookmarks.php?page=manageUser&action=manage">Zarządzaj Użytkownikami</a></div>
		</td>
		<td>		
			<?php 
			if ($action=="find"){
				$uid=null;
				if("remove" ==ParamUtils::get("subaction") && ($uid = ParamUtils::get("uid")) !=null){
					UserUtils::removeById($uid);
				}
			?>
			
			<div style="margin-left: 30px">
				<div>
					<b>Wyszukanie użytkowników</b>
				</div>
				<br/>
				
				<form action="bookmarks.php?page=manageUser&action=find" method="post"> 
					<div>Nazwa</div>
					<input type="text" name="name" value="<?php echo ParamUtils::getString("name"); ?>"/>
					
					<div>Rola użytkownika</div>
					<select name="role"> 
						<option value="<?php echo UserRoles::USER_ROLE;?>" <?php echo ParamUtils::getBoolean("role", UserRoles::USER_ROLE) ? "SELECTED" : "";?> > <?php echo UserRoles::convert(UserRoles::USER_ROLE);?> </option>
						<option value="<?php echo UserRoles::SUPER_USER_ROLE;?>" <?php echo ParamUtils::getBoolean("role", UserRoles::SUPER_USER_ROLE) ? "SELECTED" : "";?> > <?php echo UserRoles::convert(UserRoles::SUPER_USER_ROLE);?> </option>
						<option value="<?php echo UserRoles::ADMIN_ROLE;?>" <?php echo ParamUtils::getBoolean("role", UserRoles::ADMIN_ROLE) ? "SELECTED" : "";?> > <?php echo UserRoles::convert(UserRoles::ADMIN_ROLE);?> </option>
					</select>
					
					<div>Id użytkownika</div> 
					<input type="text" name="id" value="<?php echo ParamUtils::get("id");?>"/>
					
					<div>Uslugi użytkownika</div>
					<input type="checkbox" name="service" <?php echo ParamUtils::getBoolean("service") ? "CHECKED" : "";?>/>
					
					<div>Czy posiada zaległości</div>
					<input type="checkbox" name="paymentProblem" <?php echo ParamUtils::getBoolean("paymentProblem") ? "CHECKED" : "";?>/>
					
					<div>Czy zgłaszał usterki</div>
					<input type="checkbox" name="ticket" <?php echo ParamUtils::getBoolean("ticket") ? "CHECKED" : "";?>/><br/>				
					
					<input type="hidden" value="true" name="search" />
					<input type="submit" value="Szukaj" /> 
				</form>			
			</div>
			
			<?php 
				$result = null;
				if(isset($_POST['search'])){
						$controls = new UserSearchControls();
						$controls->hasTicket(ParamUtils::getBoolean("ticket"));
						$controls->hasService(ParamUtils::getBoolean("service"));
						$controls->hasPaymentProblem(ParamUtils::getBoolean("paymentProblem"));
						$controls->setName(ParamUtils::get("name"));
						$controls->setRole(ParamUtils::get("role"));
						$controls->setId(ParamUtils::get("id"));	
						$result = UserUtils::find($controls);
					if(sizeof($result>0)){
			?>
						<table>
							<tr>
								<th>Id</th>
								<th>Imię</th>
								<th>Nazwisko</th>
								<th>Telefon</th>
								<th>Adres</th>
								<th>Miasto</th>
								<th colspan="3">&nbsp;</th>
							</tr>
							<?php foreach($result as $user):?>
							<tr>
								<td><?php echo $user->getId();?></td>
								<td><?php echo $user->getName();?></td>
								<td><?php echo $user->getSurname();?></td>
								<td><?php echo $user->getPhone();?></td>
								<td><?php echo $user->getAddress();?></td>
								<td><?php echo $user->getCity();?></td>
								<td><a href = "bookmarks.php?page=manageUser&action=manage&uid=<?php echo $user->getId();?>">Zarządzaj</a></td>
								<td><a href = "bookmarks.php?page=manageUserService&action=find&uid=<?php echo $user->getId();?>">Zobacz usługi</a></td>
								<td><a href = "bookmarks.php?page=manageUser&action=find&uid=<?php echo $user->getId();?>&subaction=remove">Usun</a></td>
							</tr>
							<?php endforeach; ?>
						</table>
					<?php 
					}
				}
				
			} else{
				//Zarzadzenie uzytkownikami
				$user = new User();
				$request = "add";
				if(isset($_GET['uid']) && VariableUtils::isNumber($_GET['uid'])){
					$user = UserUtils::get($_GET['uid']);
					$request = "modify";						
				} else if(isset($_GET['request'])){
					$user->setName(ParamUtils::get("name"));
					$user->setSurname(ParamUtils::get("surname"));
					$user->setLogin(ParamUtils::get("login"));
					$user->setPassword(ParamUtils::get("password"));
					$user->setRole(ParamUtils::get("role"));
					$user->setPhone(ParamUtils::get("phone"));
					$user->setCity(ParamUtils::get("city"));
					$user->setAddress(ParamUtils::get("address"));
					if("add" == $_GET['request']){
						UserUtils::add($user);
					}else{
						$user->setId(ParamUtils::get("id"));
						UserUtils::modify($user);
					}
					$request = "modify";				
				}
				?>
				<script type="text/javascript">
						function testForm(){
								var form = jQuery("#modifyForm");
								form.find(".error").remove();
								var name = emptyValid(form.find("input[name='name']"));
								var phone = phoneValid(form.find("input[name='phone']"));
								var login = emptyValid(form.find("input[name='login']"));
								var password = emptyValid(form.find("input[name='password']"));
								return name && phone && login && password; 
						}
				
				</script>
				
				<div style ="margin-left: 20px;">
					<form action="bookmarks.php?page=manageUser&action=manage<?php echo "&request=" .$request;?>" method="post" id="modifyForm"> 
						<div>Imie</div>
						<input type="text" name="name" value="<?php echo $user->getName();?>"/>
						<div>Nazwisko</div>
						<input type="text" name="surname" value="<?php echo $user->getSurname(); ?>"/>
						<div>Telefon</div>
						<input type="text" name="phone" value="<?php echo $user->getPhone();?>"/>
						<div>Adres</div>
						<input type="text" name="address" value="<?php echo $user->getAddress();?>"/>
						<div>Miasto</div>
						<input type="text" name="city" value="<?php echo $user->getCity();?>"/>
						<div>Login</div>
						<input type="text" name="login" value="<?php echo $user->getLogin();?>"/>
						<div>Haslo</div>
						<input type="text" name="password" value="<?php echo $user->getPassword();?>"/><br/>
						
						<?php if($_SESSION['curUser']->getRole() == UserRoles::ADMIN_ROLE):?>
							<div>Rola</div>
							<select name = "role">
								<option value="<?php echo UserRoles::USER_ROLE;?>" <?php echo ParamUtils::getBoolean("role", UserRoles::USER_ROLE) || UserRoles::USER_ROLE == $user->getRole() ? "SELECTED" : "";?> > <?php echo UserRoles::convert(UserRoles::USER_ROLE);?> </option>
								<option value="<?php echo UserRoles::SUPER_USER_ROLE;?>" <?php echo ParamUtils::getBoolean("role", UserRoles::SUPER_USER_ROLE) || UserRoles::SUPER_USER_ROLE == $user->getRole() ? "SELECTED" : "";?> > <?php echo UserRoles::convert(UserRoles::SUPER_USER_ROLE);?> </option>
								<option value="<?php echo UserRoles::ADMIN_ROLE;?>" <?php echo ParamUtils::getBoolean("role", UserRoles::ADMIN_ROLE) || UserRoles::ADMIN_ROLE == $user->getRole()  ? "SELECTED" : "";?> > <?php echo UserRoles::convert(UserRoles::ADMIN_ROLE);?> </option>
							</select>
						<?php endif;?>
						<br/>
						<input type="hidden" name="id" value="<?php echo $user->getId();?>" />
						<input type="submit" value="Zapisz" onclick="return testForm()" />
					</form>
				</div>
			<?php 	
			}		
			?>
		</td>
	</tr>
</table>

